<?php

namespace app\index\controller;
use app\index\Controller;
use think\Validate;
use think\Db;
/**
 * @title 首页（模块15）
 * @description 接口说明
 * @group 用户
 * @header name:model_id require:0 default:15 desc:模块id
 * @groupremark 已使用错误代码1150001
 */
class Index extends  Controller{
    /**
     * @var array 白名单方法，如果设置会覆盖黑名单方法，只允许白名单方法能正常访问
     */
    protected static $allowList = ['test'];
    /**
     * @title 首页
     * @description 接口说明
     * @author 梁敏
     * @url /index/index/index
     * @method GET
     *
     * @param name:user_id type:int require:1 default:1 other: desc:用户ID
     * @param name:token type:int require:1 default:1 other: desc:token
     *
     * @list_messages message_id:消息ID content:消息内容
     * @return data:数据@!
     * @data notify:最新一条系统消息@! group:最新一条分组订单@! routes:路线@
     */
    public function index() {
        $data = input();
        $vmsg = [
            'token.require' => '缺少token',
            'user_id.require'   => '请填写司机的用户id',
        ];
        $validate = new Validate([
            'token'  => 'require',
            'user_id'=>"require",
        ],$vmsg);

        if (!$validate->check($data)) {
            $msg['error_id']="1150001";
            $msg['message']=$validate->getError();
            return $this->error($msg);
        }
        //系统消息
        $notify_map['isdelete']=0;
        $notify_map['user_id']=$data['user_id'];
        $notify=Db::name('notify')
            ->where($notify_map)
            ->order('id desc')
            ->find();

        //处理中的订单
        $group = Db::name('dispatch_traveler_group')
            ->alias('dtg')
            ->join('dispatch d', 'dtg.dispatch_id=d.id', 'left')
            ->where('dispatch_group_status', 'in','sendorders,proceed,achieve')
            ->where('dtg.driver_user_id', $data['user_id'])
            ->order('dtg.id desc')
            ->field('dtg.*,ifnull(dtg.end_date,0) as end_date,ifnull(dtg.go_date,0) as go_date')
            ->where('is_suspend',1)
            ->find();
        if($group){
            //人数
            $num=Db::name("dispatch_traveler")
                ->where('group_id',$group['id'])
                ->count('traveler_id');
            $group['num']=$num;
            $order_nums=Db::name("dispatch_traveler")
                ->where('group_id',$group['id'])
                ->group('oid')
                ->column('count(traveler_id) as num');
            $num_str="";
            if($order_nums){
                $num_str=implode('+',$order_nums);
            }
            $group['num_str']=$num_str;
            //旅客
            $travelers=Db::name("dispatch_traveler")
                ->alias('dt')
                ->where('dt.group_id',$group['id'])
                ->join('tp_order_traveler ot','ot.id=dt.traveler_id','left')
                ->field("ot.*")
                ->select();
            $group['travelers']=$travelers;
            // 计算天数
            $day = ($group['end_date']-$group['go_date'])/3600/24; // 求出行日期与结束日期相差几天
            if ($day && !empty($group['end_date']) && !empty($group['go_date'])){ // 出行日期和结束日期不能为空
                $go_date = date('Y-m-d', $group['go_date']);
                $date = [$go_date];
                for ($i=1; $i <= $day; $i++){
                    array_push($date,date("Y-m-d",strtotime("+{$i} day",strtotime("{$go_date}"))));
                }
                $group['day'] = count($date);
            }else{
                $group['day'] = 0;
            }
        }
        $routes = Db::table('tp_route')
            ->alias('r')
            ->join('tp_upload_file f','r.main_img=f.id','left')
            ->field('r.*,CONCAT("'.get_host().'",f.url) as url')
            ->where('status',1)
            ->where('isdelete',0)
            ->order('r.id desc')
            ->select();
        if ($routes){
            foreach ($routes as $index02 => $item02) {
//                if (!empty($item02['go_time']) && !empty($item02['end_time'])){
//                    $day = ($item02['end_time']-$item02['go_time'])/3600/24; // 求出行日期与结束日期相差几天
//                    $go_time = date('Y-m-d', $item02['go_time']);
//                    $date = [$go_time];
//                    for ($j=1; $j<=$day; $j++){
//                        array_push($date,date("Y-m-d",strtotime("+{$i} day",$item02['go_time'])));
//                    }
//                    $routes[$index02]['days'] = count($date);
//                }else{
//                    $routes[$index02]['days'] = 0;
//                }
                $routes[$index02]['days'] = empty($item02['day']) ? 0:$item02['day'];
                unset($routes[$index02]['go_time']); // 销毁出发时间
            }
        }

        // 查询客户的电话
        $phone = config('service_info');
        $result['notify']=$notify;
        $result['group']=$group;
        $result['routes']=$routes;
        $result['customer_phone']= $phone['telephone']; // 客户电话
        $msg['data']=$result;
        return $this->success($msg);
    }
    public function test1()
    {
        $client = new \swoole_client(SWOOLE_SOCK_TCP);
        if (!$client->connect('127.0.0.1', 9502, -1))
        {
            exit("connect failed. Error: {$client->errCode}\n");
        }
        $client->send("data");
        echo $client->recv();

        $client->close();
    }
    public function test()
    {





//        \app\common\model\OrderCheck::task();
        $isend=true;
        if(!$isend){
            chdir("../");
            system("php think worker:new_mfw_order -d",$res);
            dump($res);
            system("php think worker:dispatch -d",$res);
            dump($res);
            system("php think worker:mfw_order -d",$res);
            dump($res);
            system("php think worker:mfw_order_mark -d",$res);
            dump($res);
            system("php think worker:mfw_comment -d",$res);
            dump($res);
        }else{
            chdir("../");
            system("php think worker:new_mfw_order stop",$res);
            dump($res);
            system("php think worker:dispatch stop",$res);
            dump($res);
            system("php think worker:mfw_order stop",$res);
            dump($res);
            system("php think worker:mfw_order_mark stop",$res);
            dump($res);
            system("php think worker:mfw_comment stop",$res);
            dump($res);
        }

    }
}
